package com.csy.user.controller.system;

import com.csy.commons.constant.UserConstants;
import com.csy.commons.core.controller.BaseController;
import com.csy.commons.core.domain.BaseResponse;
import com.csy.commons.core.page.PageResult;
import com.csy.commons.core.utils.SecurityUtils;
import com.csy.user.entity.system.domain.SysDictType;
import com.csy.user.entity.system.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {
    @Autowired
    private SysDictTypeService sysDictTypeService;
    /*
    * 多条件分页查询字典类型数据
    * */
    @GetMapping("/list")
    public PageResult list(SysDictType dictType){
        startPage();
        List<SysDictType> list = sysDictTypeService.selectDictTypeList(dictType);
        return getData(list);
    }
    /*
    * 根据Id查询字典类型详细信息
    * */
    @GetMapping(value = "/{dictId}")
    public BaseResponse<SysDictType> getInfo(@PathVariable Long dictId){
        return BaseResponse.success(sysDictTypeService.selectDictTypeById(dictId));
    }

    /*
    * 新增字典类型
    * */
    @PostMapping
    public BaseResponse<Integer> add(@RequestBody SysDictType sysDictType){

        if (UserConstants.NOT_UNIQUE.equals(sysDictTypeService.checkDictTypeUnique(sysDictType))){
            return BaseResponse.fail("新增字典" + sysDictType.getDictName() + "失败，字典类型已经存在");
        }

        sysDictType.setCreateBy(SecurityUtils.getUserName());
        return toAjax(sysDictTypeService.insertDictType(sysDictType));
    }

    /*
     * 修改字典类型
     * */
    @PutMapping
    public BaseResponse<Integer> edit(@RequestBody SysDictType sysDictType){

        if (UserConstants.NOT_UNIQUE.equals(sysDictTypeService.checkDictTypeUnique(sysDictType))){
            return BaseResponse.fail("修改字典" + sysDictType.getDictName() + "失败，字典类型已经存在");
        }

        sysDictType.setUpdateBy(SecurityUtils.getUserName());
        return toAjax(sysDictTypeService.updateDictType(sysDictType));
    }

    /*
     * 删除字典类型
     * */
    @DeleteMapping("/{dictIds}")
    public BaseResponse<Integer> remove(@PathVariable Long[] dictIds){
        return toAjax(sysDictTypeService.deleteDictTypeByIds(dictIds));
    }

    /*
    * 清空缓存
    * */
    @DeleteMapping("/clearCache")
    public BaseResponse<String> clearCache(){
        sysDictTypeService.clearCache();
        return BaseResponse.success("清空缓存成功");
    }

}
